
//
//  PicViewController.m
//  UI_Project_A
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 GH. All rights reserved.
//


#import "PunchedLayout.h"

@implementation PunchedLayout
{
    CGSize boundsSize;
    CGFloat midX;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
{
    return YES;
}

-(void)prepareLayout
{
    [super prepareLayout];
    boundsSize = self.collectionView.bounds.size;
    midX = boundsSize.width / 2.0f;
}


- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    
    NSArray *array = [super layoutAttributesForElementsInRect:rect].mutableCopy;
    
    //    NSLog(@"------%@", NSStringFromCGRect(rect));
    
    //    NSMutableArray *copys = [NSMutableArray arrayWithCapacity:[super layoutAttributesForElementsInRect:rect].count];
    
    //    NSMutableArray *copys = [NSMutableArray arrayWithCapacity:array.count];
    //
    //    for (UICollectionViewLayoutAttributes *attris in array) {
    //        [copys addObject:[attris copy]];
    //    }
    //
    
    for (UICollectionViewLayoutAttributes* attributes in array)
    {
        
        //   [copys addObject:[attributes copy]];
        
        attributes.transform3D = CATransform3DIdentity;
        
        if (!CGRectIntersectsRect(attributes.frame, rect)) continue;
        
        CGPoint contentOffset = self.collectionView.contentOffset;
        //        NSLog(@"-----%f", contentOffset.x);
        CGPoint itemCenter = CGPointMake(attributes.center.x - contentOffset.x, attributes.center.y - contentOffset.y);
        
        CGFloat distance = ABS(midX - itemCenter.x);
        
        
        CGFloat normalized = distance / midX;
        normalized = MIN(1.0f, normalized);
        
        CGFloat zoom = cos(normalized * M_PI_4);
        
        attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 0.5f);
    }
    
    
    
    return array;
}


- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    CGFloat offsetAdjustment = CGFLOAT_MAX;
    CGRect targetRect = CGRectMake(proposedContentOffset.x , 0, boundsSize.width, boundsSize.height);
    NSArray *array = [super layoutAttributesForElementsInRect:targetRect];
    CGFloat proposedCenterX = proposedContentOffset.x + midX;
    for (UICollectionViewLayoutAttributes *layoutAttributes in array) {
        CGFloat distance = layoutAttributes.center.x - proposedCenterX;
        if (ABS(distance) < ABS(offsetAdjustment)) {
            offsetAdjustment = distance;
        }
    }
    
    CGPoint des = CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
    if ((proposedContentOffset.x == 0) || (proposedContentOffset.x >= (self.collectionViewContentSize.width - boundsSize.width))) {
        NSNotification *note = [NSNotification notificationWithName:@"PleaseRecenter" object:[NSValue valueWithCGPoint:des]];
        [[NSNotificationCenter defaultCenter] postNotification:note];
        return proposedContentOffset;
    }
    return des;
}


@end